<template>
    
    <el-card class="!border-none " shadow="never">
        
        <div class=" flex flex-1 flex-row ">
            <el-form class="-mb-4 " :model="formData" label-width="84px" inline>
                <el-form-item label="部门名称">
                    <el-input
                        class="w-64"
                        v-model="formData.name"
                        clearable
                        @keyup.enter="getPageLists"
                    >
                    
                    </el-input>
                </el-form-item>
                
                <el-form-item label="部门状态">
                    <el-select class="w-[280px]" v-model="formData.isStop">
                        <el-option label="全部" value/>
                        <el-option label="正常" value="0"/>
                        <el-option label="停用" value="1"/>
                    </el-select>
                </el-form-item>
            </el-form>
            
            
            <div class="flex-1  flex flex-row-reverse">
                <el-button class="mx-2" type="primary" @click="getPageLists">查询</el-button>
                <el-button class="mx-2" @click="resetParams">重置</el-button>
            </div>
        </div>
    </el-card>
    
    
    <el-card class="!border-none mt-5" shadow="never">
        <div>
            <el-button v-perms="['system:dept:add']" type="primary" @click="handleAdd()">
                <template #icon>
                    <icon name="el-icon-Plus"/>
                </template>
                新增
            </el-button>
            <el-button @click="handleExpand"> 展开/折叠</el-button>
        </div>
        
        
        <el-table
            ref="tableRef"
            class="mt-4"
            size="large"
            v-loading="pagerData.loading"
            :data="pagerData.lists"
            row-key="id"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
            <el-table-column label="部门名称" prop="name" min-width="150"/>
            
            <el-table-column label="部门状态" prop="isStop" min-width="100">
                <template #default="{ row }">
                    <el-tag class="ml-2" :type="row.isStop ? 'danger' : ''">
                        {{ row.isStop ? '停用' : '正常' }}
                    </el-tag>
                </template>
            </el-table-column>
            
            
            <el-table-column label="排序" prop="sort" min-width="100"/>
            <el-table-column label="更新时间" prop="updateTime" min-width="180"/>
            <el-table-column label="操作" width="160" fixed="right">
                <template #default="{ row }">
                    <el-button
                        v-perms="['system:dept:add']"
                        type="primary"
                        link
                        @click="handleAdd(row)"
                    >
                        新增
                    </el-button>
                    
                    <el-button
                        v-perms="['system:dept:edit']"
                        type="primary"
                        link
                        @click="handleEdit(row)"
                    >
                        编辑
                    </el-button>
                    
                    
                    <el-button
                        v-if="row.pid !== 0"
                        v-perms="['system:dept:del']"
                        type="danger"
                        link
                        @click="handleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    
    <edit-popup v-if="showEdit" ref="editRef" @success="getPageLists" @close="showEdit = false" />
    
</template>

<script lang='ts' setup>

import {ElTable} from "element-plus";
import EditPopup from "./edit.vue"
import {usePagerManager} from "@/manager/base/PagerManager";
import feedback from "@/utils/feedback";
import {deptDelete, getDepartmentList} from "@/api/system/SystemAuthDeptController";

const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
let isExpand = false

const formData = reactive({
    isStop: '',
    name: ''
})


/**
 * 获取分页数据
 */
const {pagerData, getPageLists, resetParams} = usePagerManager({
    fetchFun: getDepartmentList,
    params: formData
})


/**
 * 新增
 * @param row
 */
async function handleAdd(row?: any) {
    showEdit.value = true
    await nextTick()
    
    if (!!row){
        editRef.value?.setFormData({pid: row.id})
    }else {
        editRef.value?.setFormData()
    }
    editRef.value?.open('add')
}

/**
 * 编辑
 *
 * @param row
 */
async function handleEdit(row: any) {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(row)
    
}


/**
 *
 * 删除
 *
 * @param row
 */
async function handleDelete(row: any) {
    await feedback.confirm('确定要删除？')
    await deptDelete({id:row.id})
    feedback.msgSuccess('删除成功')
    getPageLists()
}


/**
 * 展开
 */
function handleExpand() {
    isExpand = !isExpand
    toggleExpand(pagerData.lists, isExpand)

}


const toggleExpand = (children: any[], unfold = true) => {
    for (const key in children) {
        tableRef.value?.toggleRowExpansion(children[key], unfold)
        if (children[key].children) {
            toggleExpand(children[key].children!, unfold)
        }
    }
}

onMounted(() => {
    getPageLists()
})


</script>

<style lang="scss" scoped>

</style>

